package com.hd.trans.ui.fragment;

import android.os.Bundle;
import android.view.MotionEvent;

import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;

public abstract class SimulBaseFragment extends Fragment {

    /**
     * 是否执行懒加载
     */
    private boolean isPrepared = false;

    /**
     * 当前Fragment是否对用户可见
     */
    private boolean isLazyLoaded = false;

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        isPrepared = true;
        lazyLoad();
    }

    @Override
    public void onResume() {
        super.onResume();
        if (isLazyLoaded) {
            onDataFresh();
        }
    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        isPrepared = false;
        isLazyLoaded = false;
    }

    @Override
    public void onHiddenChanged(boolean hidden) {
        super.onHiddenChanged(hidden);
        lazyLoad();
    }


    private void lazyLoad() {
        if (!isHidden() && isPrepared && !isLazyLoaded) {
            onlazyLoad();
            isLazyLoaded = true;
        }
    }

    //懒加载方法
    abstract void onlazyLoad();

    abstract void onDataFresh();

    public boolean onBackPressed() {
        return false;
    }

    public boolean dispatchTouchEvent(MotionEvent ev) {
        return false;
    }
}
